<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="com.c0nflict.server.*, java.util.Collection, java.util.HashSet, java.text.DecimalFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ include file="admin_header.jspf" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>c0nflict: Table Manager - Tables</title>
</head>
<body>
<%
DecimalFormat ratingFormatter = new DecimalFormat("0.00");

	Collection<Table> killTables = new HashSet<Table>();

		for (Table t : tm.tablePlayers.keySet()) {
			if (t.isQuiet() && t.getStatus() == 1
					&& t.getCurrentTurn() > 0)
				killTables.add(t);
		}

		for (Table ta : killTables) {
			tm.killTable(ta);
		}
%>

<center><h3 style="color: #ccc;"><b><%=tm.WATCHDOG.toString()%></b></h3></center>

<center><font size="5" color="white" face="verdana"><b>Tables</b></font></center>

<div class="tablesContainer">
<table class="sortable" cellpadding="4" cellspacing="0">
<thead>
<tr>
	<th width="60px">JS Id</th>
	<th width="300px">Name</th>
	<th width="60px">Round</th>
	<th width="60px">Current Turn</th>
	<th width="60px">First Turn</th>
	<th width="60px">Winning</th>
	<th width="60px">Status</th>
	<th width="60px">Quiet</th>
	<th width="60px">Players</th>
	<th width="60px">Rating<br/>(weighted)</th>
	<th width="60px">Rating<br/>(average)</th>
	<th width="60px">Rating<br/>(count)</th>
</tr>
</thead>
<tfoot>
<tr><td colspan="0">Tables in memory as of <%=tm.WATCHDOG.toString()%></td></tr>
</tfoot>
<tbody>
	<%
		int i = 1;
			String ratingAverage = "";

			for (Table tab : tm.tablePlayers.keySet()) {
				int wpid = 0;
				try {
					wpid = tab.getWinningPlayer().getOrderId();
				} catch (Exception e) {
					wpid = -1;
				}

				String dbid = "";

				if (tab.isQuiet()) {
					for (int x = 0; x < 6; x++) {
						if (!tab.players[x].isAutomaton()
								&& tab.players[x].getName() != "Empty Seat")
							dbid = tab.players[x].getName();
					}
				}
				int ratingE = 0;
				ratingAverage = "";
				int ratingVotes = 0;
				for (int z = 0; z < tm.spawnableTables.size(); z++) {
					if (tm.spawnableTables.get(z).getId() == tab.getId()) {
						ratingE = tm.spawnableTables.get(z).getRatingEstimate();
						ratingAverage = ratingFormatter.format(tm.spawnableTables.get(z).getRatingAverage());
						ratingVotes = tm.spawnableTables.get(z).getRatingVotes();
					}
				}
	%>
<tr <% if (tab.isQuiet()) { %> bgcolor="#444" <% } else if (i++ % 2 == 0) { %> bgcolor="#222" <% } %> >
	<td><%= tab.getJSID() %></td>
	<td><%= tab.getName() %></td>
	<td><%= tab.getCurrentTurn() %></td>
	<td><%= tab.getCurrentPlayerId() %></td>
	<td><%= tab.getFirstPlayerId() %></td>
	<td><%= wpid %></td>
	<td><%= tab.getStatus() %></td>
	<td><%= dbid %></td>
	<td><%= tm.tablePlayers.get(tab).size() %></td>
	<td><%= ratingE %></td>
	<td><%= ratingAverage %></td>
	<td><%= ratingVotes %></td>
</tr>
	<%
		}
	%>
</tbody>
</table>
</div>
<%@ include file="admin_footer.jspf" %>
</body>
</html>
